/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.inject;

import com.google.inject.name.Names;
import org.xptracker.persistence.PersistenceManager;
import org.xptracker.persistence.hibernate.HibernatePersistenceManager;
import static org.xptracker.util.UserConfigBase.*;
import org.xptracker.util.UserConfigClass;
import org.xptracker.util.UserConfigString;

import java.io.File;

public class HibernateModule extends BasicAppModule {

  protected void doBinding(UserConfigString dbDir, UserConfigString searchDir) {
    UserConfigClass dialect = new UserConfigClass(PROPS_FILE, HIBERNATE_DIALECT, DEFAULT_HIBERNATE_DIALECT);
    UserConfigClass driver = new UserConfigClass(PROPS_FILE, HIBERNATE_DRIVER, DEFAULT_HIBERNATE_DRIVER);

    File defaultFile = new File(dbDir.get(), DEFAULT_HIBERNATE_URL_FILE);
    UserConfigString url = new UserConfigString(PROPS_FILE, HIBERNATE_URL,
        DEFAULT_HIBERNATE_URL_BASE + defaultFile.getAbsolutePath() + DEFAULT_HIBERNATE_URL_SUFFIX);

    UserConfigString username = new UserConfigString(PROPS_FILE, HIBERNATE_USERNAME,
        DEFAULT_HIBERNATE_USERNAME);

    UserConfigString password = new UserConfigString(PROPS_FILE, HIBERNATE_PASSWORD,
        DEFAULT_HIBERNATE_PASSWORD);

    bind(Class.class).annotatedWith(Names.named("hibernate-dialect")).toProvider(dialect);
    bind(Class.class).annotatedWith(Names.named("hibernate-driver")).toProvider(driver);
    bind(String.class).annotatedWith(Names.named("hibernate-url")).toProvider(url);
    bind(String.class).annotatedWith(Names.named("hibernate-username")).toProvider(username);
    bind(String.class).annotatedWith(Names.named("hibernate-password")).toProvider(password);
    bind(String.class).annotatedWith(Names.named("hibernate-ddl-mode")).toInstance("update");
    bind(PersistenceManager.class).to(HibernatePersistenceManager.class);
  }
}
